import { setItem, getItem } from '@/utils/auth.js'
import { userProfile } from '@/api/user.js'
const state = {
  token: getItem() || '',
  userInfo: '',
  refresh_token: localStorage.getItem('refresh_token') || ''
}
const mutations = {
  setToken (state, value) {
    state.token = value
    setItem(value)
  },
  setUserInfo (state, value) {
    state.userInfo = value
  },
  // 定义mutations修改userinfo
  setUserOther (state, value) {
    state.userInfo[value.key] = value.value
  },
  // 修改refresh_token
  setRefreshToken (state, value) {
    state.refresh_token = value
    localStorage.setItem('refresh_token', value)
  }
}
const actions = {
  async getUserInfo (store) {
    // store:{state,getters.rootstate,rootgetters,comit,dispathch}
    if (store.state.userInfo === '') {
      const res = await userProfile()
      store.commit('setUserInfo', res.data)
      // console.log(res)
    }
  }
}
const getters = {
  getToken (state) {
    return state.token
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}
